Skip to content

Move methods between files and update their imports - #33

Merged
nnutter merged 6 commits into
fix-move-starship-semifrom
fix-move
Nov 5, 2026
Merged

nnutter merged 6 commits into
fix-move-starship-semifrom
fix-move

Conversation

@nnutter

@nnutter nnutter commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

No description provided.

The cross-file move condition is one bool per file, but the two
directions need different import rules. A source file can lose the only
use of an import when a declaration leaves it, while a target file can
need an import that the moved declaration uses.

Extract the file-level rule and give each direction its own predicate
that still returns the current result. The next commits relax the two
predicates separately.

Change-Id: I44f874490aa5f7b37b69dcfd0765c85c95a02159
A target file with any import used to refuse the move, which excluded
almost every real type definition file. A moved declaration only needs
the imports it actually references, so compare those references with
the names the target file already imports. A cgo target stays excluded
because its preamble sits in the import block.

The source file still has no imports, so this only lets a target file
carry imports it already uses.

Change-Id: I4a3325a9e633dfdd80b7d27da4354e274ea4de2d
A source file with any import used to refuse the move, even when the
method leaves every import in use. Compare the imports with the uses
that stay behind, and judge a file by all of the declarations that
leave it at once, because no single method can orphan an import that
another method still uses.

The move still needs the target file to provide the imports, and a
method that is the only use of an import keeps its diagnostic.

Change-Id: Id44cdafcd6cd798a1124c09bcd2c2ae71f39a169
A moved method can need an import that its new file does not have. Add
the import with the name that the method already uses, in the group
that holds the imports it sorts among, so gofmt leaves the result
alone.

A file that already binds the name to another package keeps its
diagnostic, and an import block with comments gets no new import
because the comment would no longer sit beside the import it
describes.

Judge the source file before adding anything: a method that is the only
use of an import still keeps its diagnostic, because removing imports
is not part of this fix.

Change-Id: I79b66d5e6fd565a36421e9f21e73f838e45cbf62
A method that was the only use of an import could not move, which left
most real methods in place. Give the source file the edit that removes
the import instead, so the move keeps both files compiling.

Remove a whole import declaration when it loses every import, and one
line otherwise, with the blank line that set the import apart from its
group so that gofmt leaves the file alone. Skip an import block that
holds comments, because a comment can describe the import that goes.

A removal can still end a file, so the rule that keeps a blank line
from ending a file now looks at every removal rather than only at
removed methods.

Change-Id: Ie2c72e33393da32335aa2ac46700c77482e40e9e
The README showed the checks but never the -fix option, so the rules
that decide whether a method can move were only in the code. Record
what a fix does, which imports it adds and removes, which cases it
skips, and that it only writes files that Git can restore.

Change-Id: I64853d59311a3b1a7512ac1702e2da6bc8057836
@nnutter
nnutter added this pull request to stack #34 November 5, 2026 07:36
@nnutter nnutter changed the title fix move Move methods between files and update their imports Nov 5, 2026
@nnutter
nnutter merged commit 9408314 into master Nov 5, 2026
3 checks passed
@nnutter
nnutter deleted the fix-move branch November 5, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant